package org.edu.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.common.entity.VeBaseBanJi;
import org.edu.modules.common.entity.VeBaseFaculty;
import org.edu.modules.common.mapper.VeBaseBanJiMapper;
import org.edu.modules.common.mapper.VeBaseFacultyMapper;
import org.edu.modules.common.mapper.VeBaseSpecialtyMapper;
import org.edu.modules.common.service.IVeBaseFacultyService;
import org.edu.modules.common.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther 李少君
 * @Date 2021-12-13 16:26
 */
@Service
public class VeBaseFacultyServiceImpl extends ServiceImpl<VeBaseFacultyMapper, VeBaseFaculty> implements IVeBaseFacultyService {

    @Autowired
    private VeBaseFacultyMapper veBaseFacultyMapper;

    @Autowired
    private VeBaseSpecialtyMapper veBaseSpecialtyMapper;

    @Autowired
    private VeBaseBanJiMapper veBaseBanJiMapper;

    @Override
    public List<Map<String, Object>> getFacultyPageList(VeBaseFaculty veBaseFaculty) {
        List<Map<String, Object>> list = this.veBaseFacultyMapper.getFacultyPageList(veBaseFaculty);
        if (list.size() > 0)
            for (Map<String, Object> map : list) {
                if (!"".equals(map.get("createTime")) && map.get("createTime") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("createTime").toString()));
                    map.put("createTimeName", DateTimeUtil.timestampToDate(times.longValue()));
                } else {
                    map.put("createTimeName", "");
                }
                if (!"".equals(map.get("updateTime")) && map.get("updateTime") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("updateTime").toString()));
                    map.put("updateTimeName", DateTimeUtil.timestampToDate(times.longValue()));
                    continue;
                }
                map.put("updateTimeName", "");
            }
        return list;
    }

    @Override
    public VeBaseFaculty getFacultyByName(Integer id, String name) {
        return veBaseFacultyMapper.getFacultyByName(id, name);
    }

    @Override
    public VeBaseFaculty getFacultyByCode(Integer id, String code) {
        return veBaseFacultyMapper.getFacultyByCode(id, code);
    }

    @Override
    public List<Map<String, Object>> getTreeList() {
        List<Map<String, Object>> facultyList = this.veBaseFacultyMapper.getFacultyPageList(new VeBaseFaculty());
        facultyList = setFalTimesName(facultyList);
        if (facultyList.size() > 0) {
            List<Map<String, Object>> specList = this.veBaseSpecialtyMapper.getSpecialtyAllListByFalIdList(facultyList);
            specList = setJlnyName(specList);
            List<VeBaseBanJi> banJiList = this.veBaseBanJiMapper.getBanJiListBySpecId(null, null);
            banJiList = setBanJiTimesName(banJiList);
            for (Map<String, Object> map : facultyList) {
                if (map.get("id") != null) {
                    List<Map<String, Object>> specList1 = new ArrayList<>();
                    if (specList != null && !specList.isEmpty())
                        for (Map<String, Object> model : specList) {
                            if (map.get("id").toString().equals(model.get("fal_id").toString()))
                                specList1.add(model);
                            List<VeBaseBanJi> banJiList1 = new ArrayList<>();
                            for (VeBaseBanJi banJi : banJiList) {
                                if (model.get("id") == banJi.getSpecId())
                                    banJiList1.add(banJi);
                            }
                            model.put("banJiList", banJiList1);
                        }
                    /*if (specList1.size() > 0) {
                        Map<Integer, Integer> mapList = new HashMap<>();
                        specList1.forEach(spec -> getChild(spec, mapList));
                    }*/
                    map.put("childList", specList1);
                }
            }
        }
        return facultyList;
    }

   /* public void getChild(Map<String, Object> mapModel, Map<Integer, Integer> map) {
        List<Map<String, Object>> bodyList = this.veBaseSpecialtyMapper.getSpecialtyList(null, Integer.valueOf(1));
        bodyList = setJlnyName(bodyList);
        List<VeBaseBanJi> banJiList = this.veBaseBanJiMapper.getBanJiListBySpecId(null, null);
        banJiList = setBanJiTimesName(banJiList);
        for (Map<String, Object> model : bodyList) {
            List<VeBaseBanJi> banJiList1 = new ArrayList<>();
            for (VeBaseBanJi banJi : banJiList) {
                if (model.get("id") == banJi.getSpecId())
                    banJiList1.add(banJi);
            }
            model.put("banJiList", banJiList1);
        }
        List<Map<String, Object>> childList = new ArrayList<>();
        bodyList.stream().filter(c -> !map.containsKey(c.get("id"))).filter(c -> (Integer.parseInt(c.get("pid").toString()) == Integer.parseInt(mapModel.get("id").toString())))
                .forEach(c -> {
                    map.put(Integer.valueOf(Integer.parseInt(c.get("id").toString())), Integer.valueOf(Integer.parseInt(c.get("pid").toString())));
                    getChild(c, map);
                    childList.add(c);
                });
        mapModel.put("childList", childList);
    }*/

    private List<Map<String, Object>> setFalTimesName(List<Map<String, Object>> list) {
        if (list.size() > 0)
            for (Map<String, Object> map : list) {
                if (!"".equals(map.get("createTime")) && map.get("createTime") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("createTime").toString()));
                    map.put("createTimeName", DateTimeUtil.timestampToDate(times.longValue()));
                } else {
                    map.put("createTimeName", "");
                }
                if (!"".equals(map.get("updateTime")) && map.get("updateTime") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("updateTime").toString()));
                    map.put("updateTimeName", DateTimeUtil.timestampToDate(times.longValue()));
                    continue;
                }
                map.put("updateTimeName", "");
            }
        return list;
    }

    private List<Map<String, Object>> setJlnyName(List<Map<String, Object>> list) {
        if (list.size() > 0)
            for (Map<String, Object> map : list) {
                if (!"".equals(map.get("jlny")) && map.get("jlny") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("jlny").toString()));
                    map.put("jlnyName", DateTimeUtil.timestampToDate(times.longValue()));
                    continue;
                }
                map.put("jlnyName", "");
            }
        return list;
    }

    private List<VeBaseBanJi> setBanJiTimesName(List<VeBaseBanJi> list) {
        if (list.size() > 0)
            for (VeBaseBanJi veBaseBanJi : list) {
                if (veBaseBanJi.getBysj() != null) {
                    veBaseBanJi.setBysjName(DateTimeUtil.timestampToDate(veBaseBanJi.getBysj().intValue()));
                    continue;
                }
                veBaseBanJi.setBysjName("");
            }
        return list;
    }
}
